﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LaughingDog.GameObjects;
using Microsoft.Xna.Framework;
using FarseerGames.FarseerPhysics.Factories;
using LaughingDog.Managers;

namespace LaughingDog.Components
{
    public class AngleJoint : Joint
    {
        protected FarseerGames.FarseerPhysics.Dynamics.Joints.AngleJoint mAngleJoint;
        protected RigidBody mConnectedBody = null;

        /// <summary>
        /// The second RigidBody this Spring is attached to. The first RigidBody is attached to the GameObject that the Joint is attached to.
        /// </summary>
        public RigidBody ConnectedBody { get { return mConnectedBody; } set { mConnectedBody = value; } }

        /// <summary>
        /// Gets or sets the angle in degrees that the RigidBodies will try to achieve.
        /// </summary>
        public Single TargetAngle { get { return MathHelper.ToDegrees(mAngleJoint.TargetAngle); } set { mAngleJoint.TargetAngle = MathHelper.ToRadians(value); } }

        /// <summary>
        /// Gets or sets the angle in radians that the RigidBodies will try to achieve.
        /// </summary>
        public Single TargetAngleRadians { get { return mAngleJoint.TargetAngle; } set { mAngleJoint.TargetAngle = value; } }

        /// <summary>
        /// Gets or sets the maximum torque the RigidBodies will use when attempting to achieve the TargetAngle. 
        /// </summary>
        public Single MaxImpulse { get { return mAngleJoint.MaxImpulse; } set { mAngleJoint.MaxImpulse = value; } }

        /// <summary>
        /// Creates an instance of an AngleJoint component.
        /// </summary>
        /// <param name="gameObject">The GameObject this component is attached to.</param>
        public AngleJoint(GameObject gameObject)
            : base(gameObject)
        {
            mAngleJoint = new FarseerGames.FarseerPhysics.Dynamics.Joints.AngleJoint();
            mJoint = mAngleJoint;
        }

        /// <summary>
        /// Create the Joint after setting initial settings such as the ConnectedBody.
        /// </summary>
        public override void Create()
        {
            mAngleJoint = JointFactory.Instance.CreateAngleJoint(PhysicsManager.Instance.Simulator, GameObject.RigidBody.Body, ConnectedBody.Body);
            mJoint = mAngleJoint;
        }
    }
}
